/********************************************
 * 功能说明: Web端OrganizationEmployee基础业务控制器
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2019-12-18 18:41:08
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.controller;

import com.codahale.metrics.annotation.Timed;
import com.querydsl.core.types.Predicate;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.uaa.errorcode.UaaErrorCode;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeDTO;
import com.spring.arch.common.dto.result.PageResultDTO;
import com.spring.arch.common.dto.result.ResultDTO;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.uaa.model.OrganizationEmployee;
import com.spring.arch.uaa.model.QOrganizationEmployee;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.service.OrganizationEmployeeService;
import com.spring.arch.uaa.web.converter.WebOrganizationEmployeeConverter;
import com.spring.arch.uaa.web.dto.WebOrganizationEmployeeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

import static com.spring.arch.uaa.errorcode.UaaErrorCode.E2001004001;


/**
 * Web端OrganizationEmployee基础业务控制器
 *
 * @author
 * @version 1.0.0.1
 * @since 2019-12-18 18:41:08
 */
@Slf4j
@RestController
@RequestMapping(value = "/uaa/w/organizationEmployee", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = {"Web端OrganizationEmployee管理API"})
public class WebOrganizationEmployeeController {

    @Autowired
    private OrganizationEmployeeService organizationEmployeeService;

    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;

    @Autowired
    private WebOrganizationEmployeeConverter organizationEmployeeConverter;

    /**
     * 新建操作
     *
     * @param list 新建资源的DTO
     * @return 新建资源
     */
    @Timed
    @ApiOperation(value = "新建操作")
    @PostMapping
    public ResultDTO<Void> create(@RequestBody @Valid final List<WebOrganizationEmployeeDTO> list) {
        final List<OrganizationEmployee> model = this.organizationEmployeeConverter.toListModel(list);
        this.organizationEmployeeService.saveAll(model);
        if(log.isInfoEnabled()) {
            log.info("{} instance was created.", OrganizationEmployee.class.getSimpleName());
        }
        return ResultDTO.success();
    }

    /**
     * 删除操作
     *
     * @param id 实体对象ID
     * @return 操作结果
     */
    @Timed
    @ApiOperation(value = "删除操作")
    @DeleteMapping(value = "/{id}")
    public ResultDTO<Void> delete(@PathVariable final UUID id) {
        this.organizationEmployeeService.deleteById(id);
        if(log.isInfoEnabled()) {
            log.info("{} instance {} was disable.", OrganizationEmployee.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }

    /**
     * 更新操作
     *
     * @param id                      实体对象ID
     * @param organizationEmployeeDTO 更新资源的DTO
     * @return 更新后资源
     */
    @Timed
    @ApiOperation(value = "更新操作")
    @PutMapping(value = "/{id}")
    public ResultDTO<WebOrganizationEmployeeDTO> update(@PathVariable final UUID id,
                                                        @RequestBody @Valid final WebOrganizationEmployeeDTO organizationEmployeeDTO) {
        organizationEmployeeDTO.setId(id);
        final OrganizationEmployee model = this.organizationEmployeeConverter.toModel(organizationEmployeeDTO);
        this.organizationEmployeeService.save(model);
        if(log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", OrganizationEmployee.class.getSimpleName(), model.getId());
        }
        return this.organizationEmployeeConverter.toResultDTO(model);
    }

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @Timed
    @ApiOperation(value = "获取分页数据")
    @PostMapping(value = "/s")
    public PageResultDTO<WebOrganizationEmployeeDTO> search(
            @PageableDefault(value = 10, direction = Sort.Direction.DESC) final Pageable pageable,
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "status", required = false) Boolean status,
            @RequestParam(value = "organization") UUID organization) {

        QOrganizationEmployee organizationEmployee = QOrganizationEmployee.organizationEmployee;

        Predicate predicate = organizationEmployee.organization.id.eq(organization);

        if(StringUtils.isNotBlank(code)) {

            predicate = organizationEmployee.employee.code.contains(code).and(predicate);
        }

        if(StringUtils.isNotBlank(name)) {

            predicate = organizationEmployee.employee.name.contains(name).and(predicate);
        }

        if(StringUtils.isNotBlank(mobile)) {

            predicate = organizationEmployee.employee.mobile.contains(mobile).and(predicate);
        }

        if(status != null) {

            predicate = organizationEmployee.status.eq(status).and(predicate).and(predicate);
        }

        Page<OrganizationEmployee> page = organizationEmployeeRepository.findAll(predicate, pageable);


        return organizationEmployeeConverter.toResultDTO(page);
    }

    /**
     * 取得详细数据
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "获取详细数据")
    @GetMapping(value = "/{id}")
    public ResultDTO<WebOrganizationEmployeeDTO> get(@PathVariable final UUID id) {
        final OrganizationEmployee model = this.organizationEmployeeRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001004001,
                        OrganizationEmployee.class.getSimpleName()));
        return this.organizationEmployeeConverter.toResultDTO(model);
    }

    /**
     * 锁定人员
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "锁定人员")
    @GetMapping(value = "/lock/{id}")
    public ResultDTO<WebOrganizationEmployeeDTO> lock(@PathVariable final UUID id) {
        final OrganizationEmployee model = this.organizationEmployeeRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001004001,
                        OrganizationEmployee.class.getSimpleName()));
        model.setStatus(false);
        this.organizationEmployeeService.save(model);
        return this.organizationEmployeeConverter.toResultDTO(model);
    }

    /**
     * 解锁人员
     *
     * @param id 实体对象ID
     * @return 资源详细
     */
    @Timed
    @ApiOperation(value = "解锁人员")
    @GetMapping(value = "/unlock/{id}")
    public ResultDTO<WebOrganizationEmployeeDTO> unlock(@PathVariable final UUID id) {
        final OrganizationEmployee model = this.organizationEmployeeRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(UaaErrorCode.E2001004001,
                        OrganizationEmployee.class.getSimpleName()));
        model.setStatus(true);
        this.organizationEmployeeService.save(model);
        return this.organizationEmployeeConverter.toResultDTO(model);
    }
}
